<script lang="ts" setup>
import { deleteCardAPI, getCardListAPI } from '@/apis/card';
import type { CardItem, CardListQuery } from '@/types/card';
import { ElMessage, ElMessageBox } from 'element-plus';
import { ref } from 'vue';

// 月卡信息列表 请求参数
const form = ref<CardListQuery>({
  page: 1,
  pageSize: 10,
  cardStatus: undefined,
  personName: '',
  carNumber: ''
});

// 定义月卡信息列表
const cardList = ref<CardItem[]>([]);
// 定义总数
const total = ref(0);
// 获取月卡列表信息
const getCardList = async () => {
  const res = await getCardListAPI(form.value)
  console.log(res)
  cardList.value = res.data.rows
  total.value = res.data.total
}
getCardList()

// 搜索按钮点击事件
const onSearch = () => {
  // 调用接口之前把页数参数重置为 1 
  // （防止页码在其他页时，搜索时数据不够，导致还在原来页码停留）
  form.value.page = 1
  getCardList()
}

// 删除按钮点击事件
// 创建批量删除的ids
const ids = ref('')
// 每次批量删除时，把id值保存到ids中
const selectionChange = (val: CardItem[]) => {
  console.log(val);
  ids.value = val.map(item => item.id).join(',')
}
const onDelete = (id: string) => {
  if (id === '') {
    ElMessage.warning('请选择要删除的数据')
    return
  }
  ElMessageBox.confirm('确定要删除吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      await deleteCardAPI(id)
      ElMessage.success('删除成功')
      getCardList()
    } catch (error) {
      console.log(error)
    }
  })
}
</script>

<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">车牌号码：</span>
      <el-input clearable placeholder="请输入内容" class="search-main" v-model="form.carNumber" />
      <span class="search-label">车主姓名：</span>
      <el-input clearable placeholder="请输入内容" class="search-main" v-model="form.personName" />
      <span class="search-label">状态：</span>
      <el-select style="width: 240px" v-model="form.cardStatus" clearable placeholder="全部">
        <el-option label="可用" value="0" />
        <el-option label="已过期" value="1" />
      </el-select>
      <el-button type="primary" class="search-btn" @click="onSearch">查询</el-button>
    </div>
    {{ form }}
    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <el-button type="primary" @click="$router.push('/cardAdd')">添加月卡</el-button>
      <el-button @click="onDelete(ids)">批量删除</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="cardList" @selection-change="selectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" width="100" />
        <el-table-column label="车主名称" prop="personName" />
        <el-table-column label="联系方式" prop="phoneNumber" />
        <el-table-column label="车牌号码" prop="carNumber" />
        <el-table-column label="车辆品牌" prop="carBrand" />
        <el-table-column label="剩余有效天数" prop="totalEffectiveDate" />
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="scope">
            <el-button size="small" type="primary" link>续费</el-button>
            <el-button size="small" type="primary" link>查看</el-button>
            <el-button size="small" type="primary" link @click="$router.push(`/cardAdd/${scope.row.id}`)">编辑</el-button>
            <el-button size="small" type="primary" link @click="onDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="page-container">
      <el-pagination layout="total, sizes, prev, pager, next, jumper" :total="total" v-model:current-page="form.page"
        v-model:page-size="form.pageSize" :page-sizes="[10, 20, 30]" @change="getCardList" />
    </div>
  </div>
</template>

<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-main {
    width: 220px;
    margin-right: 10px;
  }

  .search-btn {
    margin-left: 20px;
  }
}

.create-container {
  margin: 10px 0px;
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}

.form-container {
  padding: 0px 80px;
}
</style>